#include <cmath>
#include "object_builder.h"

int appendCircle(float *vertex, int start, Circle3D *circle, int numPoints) {
    int orgStart = start;
    // 三角形扇的中心
    vertex[start++] = circle->center.x;
    vertex[start++] = circle->center.y;
    vertex[start++] = circle->center.z;
    // 三角形扇的边缘
    // 每个三角形扇的角度是2*PI/numPoints
    float deltaAngle = 2 * M_PI / numPoints;
    float startAngle = 0;
    float radius = circle->radius;
    for(int i=0; i<numPoints; ++i) {
        // 第一点X,Y,Z
        vertex[start++] = circle->center.x + radius * cos(startAngle);
        vertex[start++] = circle->center.y;
        vertex[start++] = circle->center.z + radius * sin(startAngle);
        startAngle += deltaAngle;
    }
    // 最后一点与第一点相同
    vertex[start++] = vertex[orgStart + 3];
    vertex[start++] = vertex[orgStart + 4];
    vertex[start++] = vertex[orgStart + 5];
    return start;
}

int appendOpenCylinder(float *vertex, int start, Cylinder3D *cylinder, int numPoints) {
    int orgStart = start;
    // 三角形带
    float radius = cylinder->radius;
    // 每两个点2*PI/numPoints转一个角度
    float deltaAngle = 2 * M_PI / numPoints;
    float startAngle = 0;
    float halfHeight = cylinder->height / 2.f;
    for (int i=0; i<numPoints; ++i){
        // 上边缘x,y,z
        vertex[start++] = cylinder->center.x + radius * cos(startAngle);
        vertex[start++] = cylinder->center.y + halfHeight;
        vertex[start++] = cylinder->center.z + radius * sin(startAngle);
        // 下边缘x,y,z
        vertex[start++] = cylinder->center.x + radius * cos(startAngle);
        vertex[start++] = cylinder->center.y - halfHeight;
        vertex[start++] = cylinder->center.z + radius * sin(startAngle);
        // 角度增加
        startAngle += deltaAngle;
    }
    // 最后两个点重复
    for (int i=0; i<2; ++i){
        vertex[start++] = vertex[orgStart + i * 3];
        vertex[start++] = vertex[orgStart + i * 3 + 1];
        vertex[start++] = vertex[orgStart + i * 3 + 2];
    }
    return start;
}
